Use QList instead of QueueList to back RouteList.
authortsteven4 <tsteven4@gmail.com>
Sat, 9 Feb 2019 22:43:03 +0000 (15:43 -0700)
committertsteven4 <tsteven4@gmail.com>
Sat, 9 Feb 2019 22:43:03 +0000 (15:43 -0700)
This eliminates the usage of the legacy queue code for route lists.

defs.h
gtm.cc
route.cc
sort.cc
sort.h

diff --git a/defs.h b/defs.h
index b9710d6e67b892d91f53d2c97a89f60533818c47..f8cb68f392e5130c1029a6b86cf046492bab2087 100644 (file)
--- a/defs.h
+++ b/defs.h
@@ -652,7 +652,6 @@ struct computed_trkdata {
 class route_head
 {
 public:
-  queue Q;             /* Link onto parent list. */
   queue waypoint_list; /* List of child waypoints */
   QString rte_name;
   QString rte_desc;
@@ -679,17 +678,11 @@ typedef void (*route_hdr)(const route_head*);
 typedef void (*route_trl)(const route_head*);
 
 // TODO: Consider using composition instead of private inheritance.
-class RouteList : private QueueList<queue>
+class RouteList : private QList<route_head*>
 {
 public:
-// FIXME: The interface should NOT depend on the implementation of the list.
-//        Migrate to std::sort using a compare function
-//        typedef bool (*Compare)(const route_head* a, const route_head* b);
-  typedef int (*Compare)(const queue* a, const queue* b);
+  typedef bool (*Compare)(const route_head* a, const route_head* b);
 
-  RouteList();
-
-  int count() const; // a.k.a. size()
   int waypt_count() const;
   void add_head(route_head* rte); // a.k.a. append(), push_back()
   // FIXME: Generally it is inefficient to use an element pointer or reference to define the element to be deleted, use iterator instead,
@@ -719,19 +712,20 @@ public:
   // Our contained element (route_head) also contains a container (waypoint_list), 
   // and we maintain a total count the elements in these contained containers, i.e.
   // the total number of waypoints in all the routes in the RouteList.
-  using QueueList<queue>::begin;
-  using QueueList<queue>::end;
-  using QueueList<queue>::cbegin;
-  using QueueList<queue>::cend;
-  using QueueList<queue>::empty; // a.k.a. isEmpty()
-  using QueueList<queue>::front; // a.k.a. first()
-  using QueueList<queue>::back;  // a.k.a. last()
-  using QueueList<queue>::Iterator;
-  using QueueList<queue>::ConstIterator;
+  using QList<route_head*>::begin;
+  using QList<route_head*>::end;
+  using QList<route_head*>::cbegin;
+  using QList<route_head*>::cend;
+  using QList<route_head*>::empty; // a.k.a. isEmpty()
+  using QList<route_head*>::front; // a.k.a. first()
+  using QList<route_head*>::back;  // a.k.a. last()
+  using QList<route_head*>::count;  // a.k.a. size()
+  using QList<route_head*>::iterator;
+  using QList<route_head*>::const_iterator;
+  typedef iterator Iterator;
+  typedef const_iterator ConstIterator;
 
 private:
-  queue head;
-  int head_ct{0};
   int waypt_ct{0};
 };
 
@@ -789,10 +783,7 @@ template <typename T1, typename T2, typename T3>
 void
 RouteList::disp_all(T1 rh, T2 rt, T3 wc)
 {
-  queue* elem, *tmp;
-  QUEUE_FOR_EACH(&head, elem, tmp) {
-    const route_head* rhp;
-    rhp = reinterpret_cast<route_head*>(elem);
+  foreach (const route_head* rhp, *this) {
 // rh != nullptr, caught with an overload of common_disp_all
     rh(rhp);
     route_disp(rhp, wc);
@@ -805,10 +796,7 @@ template <typename T2, typename T3>
 void
 RouteList::disp_all(std::nullptr_t /* rh */, T2 rt, T3 wc)
 {
-  queue* elem, *tmp;
-  QUEUE_FOR_EACH(&head, elem, tmp) {
-    const route_head* rhp;
-    rhp = reinterpret_cast<route_head*>(elem);
+  foreach (const route_head* rhp, *this) {
 // rh == nullptr
     route_disp(rhp, wc);
 // rt != nullptr, caught with an overload of common_disp_all
@@ -820,10 +808,7 @@ template <typename T1, typename T3>
 void
 RouteList::disp_all(T1 rh, std::nullptr_t /* rt */, T3 wc)
 {
-  queue* elem, *tmp;
-  QUEUE_FOR_EACH(&head, elem, tmp) {
-    const route_head* rhp;
-    rhp = reinterpret_cast<route_head*>(elem);
+  foreach (const route_head* rhp, *this) {
 // rh != nullptr, caught with an overload of common_disp_all
     rh(rhp);
     route_disp(rhp, wc);
@@ -835,10 +820,7 @@ template <typename T3>
 void
 RouteList::disp_all(std::nullptr_t /* rh */, std::nullptr_t /* rt */, T3 wc)
 {
-  queue* elem, *tmp;
-  QUEUE_FOR_EACH(&head, elem, tmp) {
-    const route_head* rhp;
-    rhp = reinterpret_cast<route_head*>(elem);
+  foreach (const route_head* rhp, *this) {
 // rh == nullptr
     route_disp(rhp, wc);
 // rt == nullptr
diff --git a/gtm.cc b/gtm.cc
index 8360109189caac03181702831b84f0b3d3908fa1..c5ab84729198f06c405d96e139792d5f3ef44969 100644 (file)
--- a/gtm.cc
+++ b/gtm.cc
 */
 
 /*
- * Documentation can be found at http://www.trackmaker.com/download/ref_guide_eng.pdf
+ * Documentation can be found at
+ * https://www.trackmaker.com/download/ref_guide_eng.pdf
+ * https://www.trackmaker.com/download/GTM211_format.pdf
  */
 
 #include "defs.h"
 #include "jeeps/gpsmath.h"
+#include <QtCore/QList>
 
 static gbfile* file_in, *file_out;
 static int indatum;
@@ -507,11 +510,10 @@ gtm_wr_deinit()
 static void
 gtm_read()
 {
-  route_head* first_trk_head = nullptr;
   route_head* trk_head = nullptr;
   route_head* rte_head = nullptr;
   Waypoint* wpt;
-  int real_tr_count = 0;
+  QList<route_head*> real_track_list;
   unsigned int icon;
   int i;
 
@@ -575,20 +577,28 @@ gtm_read()
     if (start_new || !trk_head) {
       trk_head = route_head_alloc();
       track_add_head(trk_head);
-      real_tr_count++;
-      if (!first_trk_head) {
-        first_trk_head = trk_head;
-      }
+      real_track_list.append(trk_head);
     }
     track_add_wpt(trk_head, wpt);
   }
 
   /* Tracklog styles */
-  trk_head = first_trk_head;
-  for (i = 0; i != ts_count && i != real_tr_count; i++) {
-    trk_head->rte_name = fread_string(file_in);
+  // TODO: The format document states there are ts_count tracklog style entries,
+  //       and tr_count tracklog entries.
+  //       Some tracklog entries may be contiuation entries, so we turn these into
+  //       real_track_list.size() <= tr_count tracks.
+  //       If ts_count != real_track_list.size() we don't know how to line up the tracklogs,
+  //       and the real tracks, with the tracklog styles.
+  if (ts_count != real_track_list.size()) {
+    warning(MYNAME ": The number of tracklog entries with the new flag set doesn't match the number of tracklog style entries.");
+  }
+  for (i = 0; i != ts_count; i++) {
+    QString tname = fread_string(file_in);
     fread_discard(file_in, 12);
-    trk_head = reinterpret_cast<route_head *>QUEUE_NEXT(&trk_head->Q);
+    if (i < real_track_list.size()) {
+      trk_head = real_track_list.at(i);
+      trk_head->rte_name = tname;
+    }
   }
 
   /* Routes */
index 62ac2b4299718a314b88898a44f29f6fcdc56645..2c5c0331961338cc9aa69e46a73f07efb0be9024 100644 (file)
--- a/route.cc
+++ b/route.cc
 
 #include "defs.h"
 #include "grtcirc.h"            // for RAD, gcdist, heading_true_degrees, radtometers
-#include "queue.h"              // for queue, dequeue, QUEUE_FOR_EACH, QUEUE_MOVE, QUEUE_INIT, sortqueue, ENQUEUE_TAIL, QUEUE_EMPTY, ENQUEUE_AFTER, ENQUEUE_HEAD, QUEUE_LAST, QUEUE_NEXT, QueueList
+#include "queue.h"              // for dequeue, queue, QUEUE_FOR_EACH, QUEUE_EMPTY, ENQUEUE_HEAD, ENQUEUE_TAIL, QUEUE_INIT, QUEUE_LAST, QUEUE_NEXT
 #include "session.h"            // for curr_session, session_t (ptr only)
 #include "src/core/datetime.h"  // for DateTime
 #include "src/core/optional.h"  // for optional, operator>, operator<
 #include <QtCore/QDateTime>     // for QDateTime
+#include <QtCore/QList>         // for QList<>::iterator
 #include <QtCore/QString>       // for QString
+#include <QtCore/QtGlobal>      // for foreach
+#include <algorithm>            // for sort
 #include <cstddef>              // for nullptr_t
 
 RouteList* global_route_list;
@@ -103,37 +106,6 @@ track_insert_head(route_head* rte, route_head* predecessor)
   global_track_list->insert_head(rte, predecessor);
 }
 
-// FIXME: can we delete this unused and untested code?
-#ifdef DEAD_CODE_IS_REBORN
-static
-route_head*
-common_route_by_name(queue* routes, const char* name)
-{
-  queue* elem, *tmp;
-
-  QUEUE_FOR_EACH(routes, elem, tmp) {
-    route_head* rte = reinterpret_cast<route_head*>(elem);
-    if (rte->rte_name == name) {
-      return rte;
-    }
-  }
-
-  return nullptr;
-}
-
-route_head*
-route_find_route_by_name(const char* name)
-{
-  return common_route_by_name(&my_route_head, name);
-}
-
-route_head*
-route_find_track_by_name(const char* name)
-{
-  return common_route_by_name(&my_track_head, name);
-}
-#endif
-
 void
 route_add_wpt(route_head* rte, Waypoint* wpt, const QString& namepart, int number_digits)
 {
@@ -162,23 +134,6 @@ track_add_wpt(route_head* rte, Waypoint* wpt, const QString& namepart, int numbe
   global_track_list->add_wpt(rte, wpt, false, namepart, number_digits);
 }
 
-// FIXME: can we delete this unused and untested code?
-#ifdef DEAD_CODE_IS_REBORN
-Waypoint*
-route_find_waypt_by_name(route_head* rh, const char* name)
-{
-  queue* elem, *tmp;
-
-  QUEUE_FOR_EACH(&rh->waypoint_list, elem, tmp) {
-    Waypoint* waypointp = reinterpret_cast<Waypoint*>(elem);
-    if (waypointp->shortname == name) {
-      return waypointp;
-    }
-  }
-  return nullptr;
-}
-#endif
-
 void
 route_del_wpt(route_head* rte, Waypoint* wpt)
 {
@@ -301,44 +256,6 @@ track_sort(RouteList::Compare cmp)
   global_track_list->sort(cmp);
 }
 
-// FIXME: can we delete this unused and untested code?
-#ifdef DEAD_CODE_IS_REBORN
-/*
- * Move the entire track queue onto the route queue making no attempt
- * at all to "fix" anything in the process.
- */
-void
-routes_to_tracks()
-{
-  queue* elem, *tmp;
-
-  QUEUE_FOR_EACH(&my_route_head, elem, tmp) {
-    route_head* trk = (route_head*) elem;
-    dequeue(&trk->Q);
-    ENQUEUE_TAIL(&my_track_head, &trk->Q);
-  }
-}
-#endif
-
-// FIXME: can we delete this unused and untested code?
-#ifdef DEAD_CODE_IS_REBORN
-/*
- * Same, but in opposite direction.
- */
-void
-tracks_to_routes()
-{
-  queue* elem, *tmp;
-
-  QUEUE_FOR_EACH(&my_track_head, elem, tmp) {
-    route_head* trk = (route_head*) elem;
-    dequeue(&trk->Q);
-    ENQUEUE_TAIL(&my_route_head, &trk->Q);
-  }
-}
-#endif
-
-
 /*
  * This really makes more sense for tracks than routes.
  * Run over all the trackpoints, computing heading (course), speed, and
@@ -477,7 +394,6 @@ route_head::route_head() :
   line_width(-1),
   session(curr_session())
 {
-  QUEUE_INIT(&Q);
   QUEUE_INIT(&waypoint_list);
 };
 
@@ -489,16 +405,6 @@ route_head::~route_head()
   }
 }
 
-RouteList::RouteList() : QueueList<queue>(&head, &head_ct)
-{
-  QUEUE_INIT(&head);
-}
-
-int RouteList::count() const
-{
-  return head_ct;
-}
-
 int RouteList::waypt_count() const
 {
   return waypt_ct;
@@ -511,24 +417,23 @@ int RouteList::waypt_count() const
 void
 RouteList::add_head(route_head* rte)
 {
-  ENQUEUE_TAIL(&head, &rte->Q);
-  ++head_ct;
+  this->append(rte);
 }
 
 void
 RouteList::del_head(route_head* rte)
 {
   waypt_ct -= rte->rte_waypt_ct;
-  dequeue(&rte->Q);
+  const int idx = this->indexOf(rte);
+  removeAt(idx);
   delete rte;
-  --head_ct;
 }
 
 void
 RouteList::insert_head(route_head* rte, route_head* predecessor)
 {
-  ENQUEUE_AFTER(&predecessor->Q, &rte->Q);
-  ++head_ct;
+  const int idx = this->indexOf(predecessor);
+  this->insert(idx + 1, rte);
 }
 
 // Synthesizing names based on the total number of waypoints in the RouteList makes
@@ -564,9 +469,7 @@ RouteList::del_wpt(route_head* rte, Waypoint* wpt)
 void
 RouteList::common_disp_session(const session_t* se, route_hdr rh, route_trl rt, waypt_cb wc)
 {
-  queue* elem, *tmp;
-  QUEUE_FOR_EACH(&head, elem, tmp) {
-    const route_head* rhp = reinterpret_cast<route_head*>(elem);
+  foreach (const route_head* rhp, *this) {
     if (rhp->session == se) {
       if (rh) {
         (*rh)(rhp);
@@ -582,29 +485,24 @@ RouteList::common_disp_session(const session_t* se, route_hdr rh, route_trl rt,
 void
 RouteList::flush()
 {
-  queue* elem, *tmp;
-
-  QUEUE_FOR_EACH(&head, elem, tmp) {
-    queue* q = dequeue(elem);
-    delete reinterpret_cast<route_head*>(q);
+  foreach (route_head* rte, *this) {
+    delete rte;
   }
-  head_ct = 0;
+  clear();
   waypt_ct = 0;
 }
 
 void
 RouteList::copy(RouteList** dst) const
 {
-  queue* elem, *tmp, *elem2, *tmp2;
+  queue* elem2, *tmp2;
 
   if (*dst == nullptr) {
     *dst = new RouteList;
   }
 
   const char RPT[] = "RPT";
-  QUEUE_FOR_EACH(&head, elem, tmp) {
-    auto rte_old = reinterpret_cast<route_head*>(elem);
-
+  foreach (const route_head* rte_old, *this) {
     route_head* rte_new = route_head_alloc();
     rte_new->rte_name = rte_old->rte_name;
     rte_new->rte_desc = rte_old->rte_desc;
@@ -624,34 +522,22 @@ RouteList::restore(RouteList* src)
   if (src == nullptr) {
     return;
   }
-
   flush();
-  QUEUE_MOVE(&head, &src->head);
 
-  head_ct = src->head_ct;
-  src->head_ct = 0;
-  waypt_ct = src->waypt_ct;
+  *this = *src;
+  src->clear();
   src->waypt_ct = 0;
 }
 
 void RouteList::swap(RouteList& other)
 {
-  queue tmp_head;
-  QUEUE_MOVE(&tmp_head, &(other.head));
-  QUEUE_MOVE(&(other.head), &(this->head));
-  QUEUE_MOVE(&(this->head), &tmp_head);
-
-  const auto tmp_head_ct = other.head_ct;
-  other.head_ct = this->head_ct;
-  this->head_ct = tmp_head_ct;
-
-  const auto tmp_waypt_ct = other.waypt_ct;
-  other.waypt_ct = this->waypt_ct;
-  this->waypt_ct = tmp_waypt_ct;
+  const RouteList tmp_list = *this;
+  other = *this;
+  *this = tmp_list;
 }
 
 void RouteList::sort(Compare cmp)
 {
-  sortqueue(&head, cmp);
+  std::sort(begin(), end(), cmp);
 }
 
diff --git a/sort.cc b/sort.cc
index 1f28e243ec7f52edf7159b81af4737fa2cf7e450..1c636411b3123ad67ae860bb04a9824366732ee6 100644 (file)
--- a/sort.cc
+++ b/sort.cc
@@ -62,28 +62,19 @@ int SortFilter::sort_comp_wpt(const queue* a, const queue* b)
   }
 }
 
-int SortFilter::sort_comp_rh_by_description(const queue* a, const queue* b)
+bool SortFilter::sort_comp_rh_by_description(const route_head* a, const route_head* b)
 {
-  const route_head* x1 = reinterpret_cast<const route_head*>(a);
-  const route_head* x2 = reinterpret_cast<const route_head*>(b);
-
-  return x1->rte_desc.compare(x2->rte_desc);
+  return a->rte_desc < b->rte_desc;
 }
 
-int SortFilter::sort_comp_rh_by_name(const queue* a, const queue* b)
+bool SortFilter::sort_comp_rh_by_name(const route_head* a, const route_head* b)
 {
-  const route_head* x1 = reinterpret_cast<const route_head*>(a);
-  const route_head* x2 = reinterpret_cast<const route_head*>(b);
-
-  return x1->rte_name.compare(x2->rte_name);
+  return a->rte_name < b->rte_name;
 }
 
-int SortFilter::sort_comp_rh_by_number(const queue* a, const queue* b)
+bool SortFilter::sort_comp_rh_by_number(const route_head* a, const route_head* b)
 {
-  const route_head* x1 = reinterpret_cast<const route_head*>(a);
-  const route_head* x2 = reinterpret_cast<const route_head*>(b);
-
-  return cmp(x1->rte_num, x2->rte_num);
+  return a->rte_num <  b->rte_num;
 }
 
 int SortFilter::SortCompWptFunctor::operator()(const queue* a, const queue* b)
diff --git a/sort.h b/sort.h
index 9e42f7cf4018da762545abbc801d2f739ce34c9f..b724ecc32088bc263a691740b6bf6d353cf44367 100644 (file)
--- a/sort.h
+++ b/sort.h
@@ -108,9 +108,9 @@ private:
   };
 
   int sort_comp_wpt(const queue* a, const queue* b);
-  static int sort_comp_rh_by_description(const queue* a, const queue* b);
-  static int sort_comp_rh_by_name(const queue* a, const queue* b);
-  static int sort_comp_rh_by_number(const queue* a, const queue* b);
+  static bool sort_comp_rh_by_description(const route_head* a, const route_head* b);
+  static bool sort_comp_rh_by_name(const route_head* a, const route_head* b);
+  static bool sort_comp_rh_by_number(const route_head* a, const route_head* b);
 
   class SortCompWptFunctor
   {